package com.cangshukj.web.controller.junong;

import com.cangshukj.common.annotation.Log;
import com.cangshukj.common.core.controller.BaseController;
import com.cangshukj.common.core.domain.AjaxResult;
import com.cangshukj.common.core.page.TableDataInfo;
import com.cangshukj.common.enums.BusinessType;
import com.cangshukj.junong.domain.*;
import com.cangshukj.junong.domain.app.LandVo;
import com.cangshukj.junong.domain.app.SyVo;
import com.cangshukj.junong.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * projectName: junong_vue
 *
 * @author: 王杰
 * time: 2025/4/23 10:36
 * description:app接口
 */
@Api(value = "/app/juNong", description = "app接口")
@RestController
@RequestMapping("/app/juNong")
public class JuNongApp extends BaseController {

    @Autowired
    private ILandNewsService landNewsService;
    @Autowired
    private ILandPicsService landPicsService;
    @Autowired
    private IAgencyReginfoService agencyReginfoService;
    @Autowired
    private IAgencyTypeService agencyTypeService;
    @Autowired
    private IIncubatorsReginfoService incubatorsReginfoService;
    @Autowired
    private ILandReginfoService landReginfoService;
    @Autowired
    private ILandNatureService landNatureService;
    @Autowired
    private ISalesServiceService salesServiceService;
    @Autowired
    private IProductInfoService productInfoService;
    @Autowired
    private IProductTradeService productTradeService;
    @Autowired
    private IProductTypesService productTypesService;
    @Autowired
    private ISalesTypeService salesTypeService;

    @ApiOperation("app首页接口")
    @GetMapping("/appSy")
    public AjaxResult appSy(SyVo syVo){

        List<LandPics> landVo = syVo.getLands();
        List<LandPics> productVo = syVo.getProducts();
        List<LandPics> saleVo = syVo.getSales();
        List<LandPics> agencyVo = syVo.getAgencys();
        //土地流转
        int land = 0;
        List<LandNews> landNew = landNewsService.selectLandNewsType(land);
        for (LandNews l:landNew){
            String id = l.getId();
            String tableName = "land_news";
            List<LandPics> lands = landPicsService.selectLandPicsByFile(id, tableName);
            for (LandPics pic:lands){
                pic.setTitle(l.getTitle());
                landVo.add(pic);
                break;
            }
        }
        //农产品
        List<ProductInfo> productInfos = productInfoService.selectProductInfoList(new ProductInfo());
        for (ProductInfo l:productInfos){
            String id = l.getId();
            String tableName = "product_info";
            List<LandPics> lands = landPicsService.selectLandPicsByFile(id, tableName);
            for (LandPics pic:lands){
                pic.setTitle(l.getTitle());
                productVo.add(pic);
                break;
            }
        }
        //销售服务
        List<SalesService> salesServices = salesServiceService.selectSalesServiceList(new SalesService());
        for (SalesService l:salesServices){
            String id = l.getId();
            String tableName = "sales_service";
            List<LandPics> lands = landPicsService.selectLandPicsByFile(id, tableName);
            for (LandPics pic:lands){
                pic.setTitle(l.getTitle());
                saleVo.add(pic);
                break;
            }
        }
        //代理服务
        int agency = 3;
        List<LandNews> agencyNew = landNewsService.selectLandNewsType(agency);
        for (LandNews l:agencyNew){
            String id = l.getId();
            String tableName = "land_news";
            List<LandPics> lands = landPicsService.selectLandPicsByFile(id, tableName);
            for (LandPics pic:lands){
                pic.setTitle(l.getTitle());
                agencyVo.add(pic);
                break;
            }
        }
        syVo.setAgencys(agencyVo);
        syVo.setLands(landVo);
        syVo.setProducts(productVo);
        syVo.setSales(saleVo);
        return AjaxResult.success(syVo);
    }

    @ApiOperation("土地流转详情")
    @GetMapping("/landInfo")
    public AjaxResult landInfo(LandVo vo){

        List<LandReginfo> landInfos = landReginfoService.selectLandReginfoList(new LandReginfo());
        for (LandReginfo info:landInfos){
            String natureId = info.getNatureId();
            LandNature landNature = landNatureService.selectLandNatureByNatureId(natureId);
            info.setNatureName(landNature.getNatureName());
        }
        vo.setLandInfos(landInfos);

        List<LandPics> landPics = vo.getLandPics();
        int land = 0;
        List<LandNews> landNew = landNewsService.selectLandNewsType(land);
        for (LandNews l:landNew){
            String id = l.getId();
            String tableName = "land_news";
            List<LandPics> lands = landPicsService.selectLandPicsByFile(id, tableName);
            for (LandPics pic:lands){
                pic.setTitle(l.getTitle());
                landPics.add(pic);
                break;
            }
        }
        vo.setLandPics(landPics);
        return AjaxResult.success(vo);
    }

    /**
     * 新增土地流转登记信息
     */
    @ApiOperation("新增土地流转登记信息")
    @PostMapping("/land")
    public AjaxResult add(@RequestBody LandReginfo landReginfo)
    {
        landReginfo.setCreateBy("网页提交");
        landReginfo.setCreateTime(new Date());
        return toAjax(landReginfoService.insertLandReginfo(landReginfo));
    }

    @ApiOperation("查询土地流转性质不分页")
    @GetMapping("/listNoPage")
    public AjaxResult listNoPage(LandNature landNature)
    {
        List<LandNature> list = landNatureService.selectLandNatureList(landNature);
        return AjaxResult.success(list);
    }

    @ApiOperation("农产品详情")
    @GetMapping("/productInfo")
    public AjaxResult productInfo(LandVo vo){

        List<ProductTrade> productTrades = productTradeService.selectProductTradeList(new ProductTrade());
        vo.setTrades(productTrades);

        List<LandPics> landPics = vo.getLandPics();
        int product = 1;
        List<LandNews> productNew = landNewsService.selectLandNewsType(product);
        for (LandNews l:productNew){
            String id = l.getId();
            String tableName = "land_news";
            List<LandPics> lands = landPicsService.selectLandPicsByFile(id, tableName);
            for (LandPics pic:lands){
                pic.setTitle(l.getTitle());
                landPics.add(pic);
                break;
            }
        }
        vo.setLandPics(landPics);

        //农产品信息
        List<ProductInfo> productInfos = productInfoService.selectProductInfoList(new ProductInfo());
        for (ProductInfo info:productInfos){
            String id = info.getId();
            String tableName = "product_info";
            List<LandPics> lands = landPicsService.selectLandPicsByFile(id, tableName);
            info.setProducts(lands);
        }
        vo.setInfos(productInfos);
        return AjaxResult.success(vo);
    }

    /**
     * 查询农产品信息列表
     */
    @ApiOperation("查询农产品信息列表")
    @GetMapping("/productInfoList")
    public TableDataInfo productInfoList(ProductInfo productInfo)
    {
        startPage();
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        for (ProductInfo info:list){
            String id = info.getId();
            String tableName = "product_info";
            List<LandPics> lands = landPicsService.selectLandPicsByFile(id, tableName);
            info.setProducts(lands);
        }
        clearPage();
        return getDataTable(list);
    }

    /**
     * 查询农产品分类列表(不分页)
     */
    @ApiOperation("查询农产品分类列表(不分页)")
    @GetMapping("/productListNoPage")
    public AjaxResult productListNoPage(ProductTypes productTypes)
    {
        List<ProductTypes> list = productTypesService.selectProductTypesList(productTypes);
        return AjaxResult.success(list);
    }

    /**
     * 新增农产品供求信息
     */
    @ApiOperation("新增农产品供求信息")
    @PostMapping("/product")
    public AjaxResult add(@RequestBody ProductTrade productTrade)
    {
        productTrade.setCreateBy("网页提交");
        productTrade.setCreateTime(new Date());
        return toAjax(productTradeService.insertProductTrade(productTrade));
    }

    @ApiOperation("销售服务详情")
    @GetMapping("/saleInfo")
    public AjaxResult saleInfo(LandVo vo){
        List<SalesService> salesServices = salesServiceService.selectSalesServiceList(new SalesService());
        for (SalesService service:salesServices){
            String id = service.getId();
            String tName = "sales_service";
            List<LandPics> landPics = landPicsService.selectLandPicsByFile(id, tName);
            service.setSales(landPics);
            continue;
        }
        vo.setSales(salesServices);
        List<LandPics> landPics = vo.getLandPics();
        int sale = 2;
        List<LandNews> saleNew = landNewsService.selectLandNewsType(sale);
        for (LandNews l:saleNew){
            String id = l.getId();
            String tableName = "land_news";
            List<LandPics> lands = landPicsService.selectLandPicsByFile(id, tableName);
            for (LandPics pic:lands){
                pic.setTitle(l.getTitle());
                landPics.add(pic);
                break;
            }
        }
        vo.setLandPics(landPics);
        return AjaxResult.success(vo);
    }
    @ApiOperation("代理服务详情")
    @GetMapping("/agencyInfo")
    public AjaxResult agencyInfo(LandVo vo){
        List<LandPics> landPics = vo.getLandPics();
        int agency = 3;
        List<LandNews> agencyNew = landNewsService.selectLandNewsType(agency);
        for (LandNews l:agencyNew){
            String id = l.getId();
            String tableName = "land_news";
            List<LandPics> lands = landPicsService.selectLandPicsByFile(id, tableName);
            for (LandPics pic:lands){
                pic.setTitle(l.getTitle());
                landPics.add(pic);
                break;
            }
        }
        vo.setLandPics(landPics);
        return AjaxResult.success(vo);
    }

    /**
     * 新增代理服务登记信息
     */
    @ApiOperation("新增代理服务登记信息")
    @PostMapping("/agency")
    public AjaxResult add(@RequestBody AgencyReginfo agencyReginfo)
    {
        agencyReginfo.setCreateBy("网页提交");
        agencyReginfo.setCreateTime(new Date());
        return toAjax(agencyReginfoService.insertAgencyReginfo(agencyReginfo));
    }

    @ApiOperation("孵化基地详情")
    @GetMapping("/incubatorInfo")
    public AjaxResult incubatorInfo(LandVo vo){
        List<IncubatorsReginfo> incubatorsReginfos = incubatorsReginfoService.selectIncubatorsReginfoList(new IncubatorsReginfo());
        vo.setIncubators(incubatorsReginfos);

        List<LandPics> landPics = vo.getLandPics();
        int incubator = 4;
        List<LandNews> agencyNew = landNewsService.selectLandNewsType(incubator);
        for (LandNews l:agencyNew){
            String id = l.getId();
            String tableName = "land_news";
            List<LandPics> lands = landPicsService.selectLandPicsByFile(id, tableName);
            for (LandPics pic:lands){
                pic.setTitle(l.getTitle());
                landPics.add(pic);
                break;
            }
        }
        vo.setLandPics(landPics);
        return AjaxResult.success(vo);
    }

    /**
     * 新增孵化地基-登记信息
     */
    @ApiOperation("新增孵化地基-登记信息")
    @PostMapping("/incubator")
    public AjaxResult add(@RequestBody IncubatorsReginfo incubatorsReginfo)
    {
        incubatorsReginfo.setCreateBy("网页提交");
        incubatorsReginfo.setCreateTime(new Date());
        return toAjax(incubatorsReginfoService.insertIncubatorsReginfo(incubatorsReginfo));
    }

    /**
     * 查询土地流转登记信息列表
     */
    @ApiOperation("查询土地流转登记信息列表")
    @GetMapping("/landList")
    public TableDataInfo landList(LandReginfo landReginfo)
    {
        startPage();
        List<LandReginfo> list = landReginfoService.selectLandReginfoList(landReginfo);
        for (LandReginfo info:list){
            String natureId = info.getNatureId();
            LandNature landNature = landNatureService.selectLandNatureByNatureId(natureId);
            if(landNature != null){
                info.setNatureName(landNature.getNatureName());
            }
        }
        clearPage();
        return getDataTable(list);
    }

    /**
     * 查询新闻信息列表
     */
    @ApiOperation("查询新闻信息列表")
    @GetMapping("/newList")
    public TableDataInfo newList(LandNews landNews)
    {
        startPage();
        List<LandNews> list = landNewsService.selectLandNewsList(landNews);
        for (LandNews pic:list){
            String id = pic.getId();
            List<LandPics> pics= landPicsService.selectLandPicsByFile(id, "land_news");
            pic.setLandNews(pics);
        }

        clearPage();
        return getDataTable(list);
    }

    /**
     * 查询代理服务登记信息列表
     */
    @ApiOperation("查询代理服务登记信息列表")
    @GetMapping("/agencyList")
    public TableDataInfo agencyList(AgencyReginfo agencyReginfo)
    {
        startPage();
        List<AgencyReginfo> list = agencyReginfoService.selectAgencyReginfoList(agencyReginfo);
        if (list.size() > 0){
            for (AgencyReginfo info:list){
                String typeId = info.getTypeId();
                AgencyType agencyType = agencyTypeService.selectAgencyTypeByTypeId(typeId);
                info.setTypeName(agencyType.getTypeName());
            }
        }
        clearPage();
        return getDataTable(list);
    }

    /**
     * 查询销告服务服务信息列表
     */
    @ApiOperation("查询销告服务服务信息列表")
    @GetMapping("/saleList")
    public TableDataInfo saleList(SalesService salesService)
    {
        startPage();
        List<SalesService> list = salesServiceService.selectSalesServiceList(salesService);
        if (list.size() > 0){
            for (SalesService service:list){
                String typeId = service.getTypeId();
                SalesType salesType = salesTypeService.selectSalesTypeByTypeId(typeId);
                service.setTypeName(salesType.getTypeName());
                String id = service.getId();
                List<LandPics> landNews = landPicsService.selectLandPicsByFile(id, "sales_service");
                service.setSales(landNews);
            }
        }
        clearPage();
        return getDataTable(list);
    }
    @ApiOperation("查询销告服务分类列表（pid = 0）")
    @GetMapping("/saleTypeByPid")
    public AjaxResult saleTypeByPid(){

        List<SalesType> salesTypes = salesTypeService.selectSalesTypeByPid("0");
        return AjaxResult.success(salesTypes);
    }

    @ApiOperation("查询销告服务分类列表（子集）")
    @GetMapping("/saleType")
    public AjaxResult saleType(String typeId){
        List<SalesType> salesTypes = salesTypeService.selectSalesTypeByPid(typeId);
        return AjaxResult.success(salesTypes);
    }




    /**
     * 查询农产品信息列表
     */
    @ApiOperation("查询农产品信息列表")
    @GetMapping("/productList")
    public TableDataInfo productList(ProductTrade productTrade)
    {
        startPage();
        List<ProductTrade> list = productTradeService.selectProductTradeList(productTrade);
        clearPage();
        return getDataTable(list);
    }

    /**
     * 查询孵化地基-登记信息列表
     */
    @ApiOperation("查询孵化地基-登记信息列表")
    @GetMapping("/incubatorList")
    public AjaxResult incubatorList(IncubatorsReginfo incubatorsReginfo)
    {
        IncubatorsReginfo reginfo = incubatorsReginfoService.selectIncubators(incubatorsReginfo);
        return AjaxResult.success(reginfo);
    }

    /**
     * 获取新闻信息详细信息
     */
    @ApiOperation("获取新闻信息详细信息")
    @GetMapping(value = "/newGetInfo")
    public AjaxResult newGetInfo(String id)
    {
        LandNews landNews = landNewsService.selectLandNewsById(id);
        String dataid = landNews.getId();
        String tablename = "land_news";
        List<LandPics>  pics = landPicsService.selectLandPicsByFile(dataid,tablename);
        landNews.setLandNews(pics);
        return AjaxResult.success(landNews);
    }

    /**
     * 获取农产品信息详细信息
     */
    @ApiOperation("获取农产品信息详细信息")
    @GetMapping(value = "/productGetInfo")
    public AjaxResult productGetInfo(String id)
    {
        ProductInfo productInfo = productInfoService.selectProductInfoById(id);
        String dataid = productInfo.getId();
        String tablename = "product_info";
        List<LandPics>  pics = landPicsService.selectLandPicsByFile(dataid, tablename);
        productInfo.setProducts(pics);
        return AjaxResult.success(productInfo);
    }

    /**
     * 获取农产品供求信息详细信息
     */
    @ApiOperation("获取农产品供求信息详细信息")
    @GetMapping(value = "/tradeGetInfo")
    public AjaxResult tradeGetInfo(String id)
    {
        return AjaxResult.success(productTradeService.selectProductTradeById(id));
    }

    /**
     * 获取销告服务服务信息详细信息
     */
    @ApiOperation("获取销告服务服务信息详细信息")
    @GetMapping(value = "/saleGetInfo")
    public AjaxResult saleGetInfo(String id)
    {
        SalesService salesService = salesServiceService.selectSalesServiceById(id);
        String dataid = salesService.getId();
        String tablename = "sales_service";
        List<LandPics>  pics = landPicsService.selectLandPicsByFile(dataid,tablename);
        salesService.setSales(pics);
        return AjaxResult.success(salesService);
    }

    @ApiOperation("获取代理服务登记信息详细信息")
    @GetMapping(value = "/agencyGetInfo")
    public AjaxResult agencyGetInfo(String id)
    {
        return AjaxResult.success(agencyReginfoService.selectAgencyReginfoById(id));
    }

}
